package erwiki.ui.app.dialogs;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
//import org.elwiki.app.internal.ImageResource;

public class LoginDialog extends Dialog {

	private static final long serialVersionUID = -1434734232356750619L;

	private Text textUser;
	private Text textPassword;

	private String userName;

	private String password;

	/**
	 * Create the dialog.
	 *
	 * @param parentShell
	 */
	public LoginDialog(Shell parentShell) {
		super(parentShell);
		setShellStyle(SWT.TITLE | SWT.APPLICATION_MODAL);
		// TODO: define image.
		// super.setDefaultImage(ImageResource.getImage(ImageResource.IMG_DOOR));

		// Get service

//		if (dsRef != null) {
//			loginService = (LoginService) context.getService(dsRef);
//		}
	}

	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("Вход в систему");
	}

	/**
	 * Create contents of the dialog.
	 *
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		container.setLayout(new GridLayout(2, false));

		{
			Label lblUser = new Label(container, SWT.NONE);
			lblUser.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
			lblUser.setText("Пользователь:");

			this.textUser = new Text(container, SWT.BORDER);
			this.textUser.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		}

		{
			Label lblPassword = new Label(container, SWT.NONE);
			lblPassword.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false, 1, 1));
			lblPassword.setText("Пароль:");
		}
		{
			this.textPassword = new Text(container, SWT.BORDER | SWT.PASSWORD);
			this.textPassword.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		}

		// WORKAROUND.
		this.textUser.setText("vfedorov");
		this.textPassword.setText("password");

		return container;
	}

	/**
	 * Create contents of the button bar.
	 *
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		Button btn = createButton(parent, IDialogConstants.OK_ID, "Войти", true);
		// TODO: define image.
		// btn.setImage(ImageResource.getImage(ImageResource.IMG_DOOR_IN));
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(400, 250);
	}

	@Override
	protected void okPressed() {
		this.userName = this.textUser.getText();
		this.password = this.textPassword.getText();

		super.okPressed();
	}

	public String getUserName() {
		return this.userName;
	}

	public String getPassword() {
		return this.password;
	}
}
